<!-- 右边图标跳转 页面 -->
<template>
	<view class="">
		<view class="search-content  flex">
			<image class="search-img" src="/static/sousuo.jpg"></image>
			<input placeholder="搜索商品" v-model="searchVal" @input="changeInput" @confirm="changeSearch"
				placeholder-style="color:#8F8F8F;" />
			<image v-if="searchVal" class="icon_close" src="/static/delect.png" @click="close()"></image>
		</view>


		<view class="searchBox">
			<view class="flex-between"v-if="historyList.length">
				<view class="">
					搜索历史
				</view>
				<view class="flex" style="color: #DE4040;" @click="cleanUp()" >
					清除
					<uni-icons color="#DE4040" type="trash" size="24"></uni-icons>

				</view>
			</view>
			<view class="flex-wrap-flex-start">
				<view class="history flex" v-for="(item,index) in historyList" :key="index">
					<text style="margin-right: 10rpx;" @click="historyFn(item)">{{item}}</text>
					<uni-icons @click="del(item)" type="closeempty" color="#666666" size="14"></uni-icons>
				</view>

			</view>
			<view class="margin_25rpx_0">
				热门搜索
			</view>
			<view class="">
				<view class="padding_30rpx_0" v-for="(item,index) in typeList" :key="index">
					<view class="flex-between" @click="toDetail(item.id)">
						<view class="" style="color: #666666;">
							{{item.name}}
						</view>
						<view class="">
							<uni-icons color="#989696" type="right" size="20"></uni-icons>

						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				historyList: [],
				searchList: [],
				typeList: [],
				searchVal: '',
				timeout: ''
			};
		},
		onLoad() {
			this.goodsList()
			this.getProtocol()
		},
		onPageScroll(ev) {


		},
		methods: {
			historyFn(item){
				this.searchVal=item
				this.goodsList()
			},
			async del(label){
				const result = await this.$http.get({
					url: this.$api.searchDel,
					data: {
						uid: uni.getStorageSync('uid'),
						label:label
					}
				});
				if (result.errno == 0) {
					this.$sun.toast(result.message);
					this.getProtocol()
				} else {
					this.$sun.toast(result.message);
				}
			},
			cleanUp() {
				uni.showModal({
					title: '提示',
					content: '是否删除所有历史记录',
					success: async (res) => {
						if (res.confirm) {

							const result = await this.$http.get({
								url: this.$api.searchDel,
								data: {
									uid: uni.getStorageSync('uid'),
									label:''
								}
							});
							if (result.errno == 0) {
								this.$sun.toast(result.message);
								this.getProtocol()
							} else {
								this.$sun.toast(result.message);
							}
						} else if (res.cancel) {

						}
					}
				});

			},
			// '普通商品'详情
			toDetail(id) {
				this.$sun.navGo('/pages/detail/detail', {
					goods_id: id,
					type: 0
				});
			},
			close() {
				this.searchVal = ''
				this.goodsList()
				this.getProtocol()
			},
			changeInput(e) {


				clearTimeout(this.timeout);

				// 设置新的定时器，只有停止输入 300 毫秒后才执行搜索
				this.timeout = setTimeout(() => {
					this.searchVal = e.detail.value
					this.goodsList()
					console.log(e.detail.value)
					console.log('Searching for:'); // 执行搜索操作
				}, 1500); // 设定 300 毫秒的节流时间

			},
			changeSearch() {
				this.goodsList()
			},
			async getProtocol() {
				const result = await this.$http.get({
					url: this.$api.mallSet,
					data: {
						uid: uni.getStorageSync('uid'),
					}
				});
				if (result.errno == 0) {
					this.historyList = result.data.search;
					console.log('商城设置_协议', result.data);
				} else {
					this.$sun.toast(result.message);
				}
			},
			async goodsList() {

				const result = await this.$http.get({
					url: this.$api.goodsList,
					data: {
						page: 1,
						psize: 10,
						name: this.searchVal,
						// 人气热卖
						sold: 2,
						// 最新发布
						create_time: 2,
						uid: uni.getStorageSync('uid'),

					}
				});
				if (result.errno == 0) {


					this.typeList = (result.data.list);
					// if (this.distributionSet.distribution_level != 0) {
					// 	this.getChange2(0);
					// }
				}
			},
		}
	};
</script>

<style lang="less" scoped>
	.search-content {
		background: #eceaea;
		margin: 20rpx;
		height: 76rpx;

		.search-img {
			width: 30rpx;
			height: 30rpx;
			margin: 0 10rpx 0 30rpx;
		}

		input {
			flex: 1;
		}

		.icon_close {
			padding: 0rpx 15rpx;
			width: 37rpx;
			height: 37rpx;
		}

		border-radius: 164rpx;

		background: rgb(255, 255, 255);
	}

	.searchBox {
		padding: 20rpx;
		margin: 0 20rpx;
		border-radius: 20rpx;

		background: rgb(255, 255, 255);

		.history {
			margin: 20rpx 20rpx 20rpx 0;
			// width: 130rpx;
			padding: 20rpx;
			height: 60rpx;
			border-radius: 10rpx;
			color: #666666;
			background: rgb(247, 247, 247);
		}
	}
</style>